<template>
    <!-- 续期-提核保单录入 -->
    <div class="fillcontain">

        <el-dialog
                title="提核保单录入"
                :visible.sync="drawer"
                :direction="direction"
                :width="'70%'"
                :before-close="handleClose"
                center>
            <div class="contain editInput" >
                <div class="table_container">
                    <el-form :model="tableData[0]" ref="dynamicValidateForm" :rules="rules">
                        <el-table ref="multipleTable" v-loading="loading" :data="tableData" tooltip-effect="dark"
                              highlight-current-row header-cell-class-name="table-header-class"
                              style="width:100%">
                        <el-table-column label="保单号码" align='center'>
                            <template slot-scope="scope">
                                <el-form-item prop="chdrnum"  >
                                    <el-input clearable v-model="scope.row.chdrnum" placeholder="保单号码" ></el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column label="Eff Date" align='center'>
                            <template slot-scope="scope">
                                <el-form-item prop="effdate01"  >
                                     <el-input clearable v-model="scope.row.effdate01" placeholder="Eff Date" ></el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column label="提核原因" align='center'>
                            <template slot-scope="scope">
                                <el-form-item prop="bmntdesc" >
                                      <el-input clearable v-model="scope.row.bmntdesc" placeholder="提核原因"></el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column label="被保人客户号" align='center'>
                            <template slot-scope="scope">
                                <el-form-item prop="cownnum"  >
                                   <el-input clearable v-model="scope.row.cownnum" placeholder="被保人客户号" :readonly="true" :disabled="true"></el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column label="案件类型" align='center'>
                            <template slot-scope="scope">
                                <el-form-item prop="archtype" >
                                    <el-select clearable v-model="scope.row.archtype" placeholder="案件类型">
                                        <el-option :label="'4-人工抽查核保'" :value="'4'"></el-option>
                                    </el-select>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column label="提核人员" align='center'>
                            <template slot-scope="scope">
                                <el-form-item prop="userid" >
                                    <el-input clearable v-model="scope.row.userid" placeholder="提核人员" :readonly="true" :disabled="true"></el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column label="提核日期" align='center'>
                            <template slot-scope="scope">
                                <el-form-item prop="effdate03" >
                                    <el-input clearable v-model="scope.row.effdate03" placeholder="提核日期" :readonly="true" :disabled="true"></el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column label="处理注记" align='center'>
                            <template slot-scope="scope">
                                <el-form-item prop="flag01" >
                                      <el-select clearable v-model="scope.row.flag01" placeholder="处理注记">
                                          <el-option :label="'online抽取'" :value="'1'"></el-option>
                                          <el-option :label="'bath提取'" :value="'0'"></el-option>
                                      </el-select>
                                </el-form-item>
                            </template>
                        </el-table-column>
                    </el-table>
                        <el-table ref="textTable" v-loading="loading" :data="tableData" tooltip-effect="dark" :show-header="false"
                                  style="width:100%">
                            <el-table-column  align='center'>
                                <template slot-scope="scope">
                                    <el-form-item prop="bmntdesc" >
                                        <el-input clearable v-model="scope.row.bmntdesc" placeholder="提核原因" type="textarea"
                                                  :autosize="{ minRows: 3, maxRows: 6}"></el-input>
                                    </el-form-item>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-form>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button type="danger" @click="handleClose">取消</el-button>
                <el-button type="danger" class="Defaultbutton" @click="validateSubmit">保存</el-button>
            </span>

            <!-- <el-row >
                <el-col :span="12" class="pb15 pt5 text_center">
                    <el-button type="danger" @click="handleClose">取消</el-button>
                </el-col>
                <el-col :span="12" class="pb15 pt5 text_center">
                    <el-button type="danger" class="Defaultbutton" @click="validateSubmit">保存</el-button>
                </el-col>
            </el-row> -->
        </el-dialog>
    </div>
</template>

<script>
    import {mapGetters} from 'vuex'
    import {getLogonUserAndGlBusinessDate,checkChdrnumFromChdrenqByChdrcoyChdrnum,
        callEUIS044,findMinRrtdatFromCovrByChdrnum,countEuruByChdrcoyRrtdatChdrnum
    ,findMinFlag01EuruByChdrcoyRrtdatChdrnum,findClmnumEuruByChdrcoyRrtdatChdrnum
    ,addRenewal} from "@/api/taskCenter/renewalTask";
    import constant from '@/utils/constant';


    export default {
        name: "add-renewal",
        data() {
            //保单号
            var validateChdrnum = (rule, value, callback) => {
                if (/^[0-9]{8}$/.test(value) == false) {
                    callback(new Error("请输入正确保单号"));
                } else {
                    //保单是否存在
                    checkChdrnumFromChdrenqByChdrcoyChdrnum(this.tableData[0].chdrnum,this.usercomp).then(res => {
                        if (res.resultCode == constant.SUCCESS && res.data) {
                            if(res.data.statcode !='IF' && res.data.statcode !='MP' && res.data.statcode !='AP'){
                                callback(new Error("保单状态错误"));
                            }else {
                                //取保资料
                                callEUIS044(this.tableData[0].chdrnum,this.usercomp).then(res => {
                                    if (res.resultCode == constant.SUCCESS && res.data) {
                                        if((res.data.statuz.trim().length==0||res.data.statuz.trim()=='****')){
                                            this.tableData.forEach(item => {
                                                if (value === item.chdrnum){
                                                    item.cownnum = res.data.cownnum;
                                                }
                                            });
                                            callback();
                                            this.findMinRrtdatFromCovrByChdrnum();
                                        }else {
                                            callback(new Error("取保单资料错误,请重新输入"));
                                        }
                                    }else {
                                        callback(new Error(res.resultMsg));
                                    }
                                }).catch(err => {
                                    callback(new Error("校验取保单资料异常"));
                                })
                            }
                        }else {
                            callback(new Error(res.resultMsg));
                        }
                    }).catch(err => {
                        callback(new Error("校验保单号异常"));
                    })
                }
            };
            return {
                // 查询条件
                direction: 'rtl',
                tableData: [],
                userid:"",//用户id
                ldaDate:"",//存储过程时间
                usercomp:"",//当前用户公司
                loading: false,
                showAdd: false,
                validateChdrnum:validateChdrnum,
                pickerOptions1: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    }
                },
                rules: {
                    chdrnum: [{ required: true, message: '请输入保单号码', trigger: 'blur' },  { validator: validateChdrnum,  trigger: ['blur', 'change'] } ],
                    effdate01: [{ required: true, message: '请输入Eff Date', trigger: 'blur' } ],
                    bmntdesc: [{ required: true, message: '请输入提核原因', trigger: 'blur' } ],
                    cownnum: [{ required: true, message: '请输入被保人客户号', trigger: 'blur' } ],
                    archtype: [{ required: true, message: '请选择案件类型', trigger: 'blur' } ],
                    userid: [{ required: true, message: '请输入提核人员', trigger: 'blur' } ],
                    effdate03:[{ required: true, message: '请输入提核日期', trigger: 'blur' } ],
                    flag01: [{ required: true, message: '请选择处理注记', trigger: 'blur' } ],

                },
            };

        },
        created() {

        },
        mounted() {
            this.getLogonUserAndGlBusinessDate();
        },
        props: {
            drawer: {
                type: Boolean,
                default: false
            },
            taskNo: {
                type: String
            },
        },
        methods: {
            handleClose(done) {
                this.clearRow();
                this.$emit('close', this.drawer);
            },
            //获取当前用户的信息以及存储过程的时间
            getLogonUserAndGlBusinessDate(){
                this.$store.dispatch('loading/CHANGE_LOADING',true);
                getLogonUserAndGlBusinessDate().then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if (res.resultCode == constant.SUCCESS && res.data) {
                         this.userid = res.data.userid;
                        this.ldaDate = res.data.ldaDate;
                        this.usercomp = res.data.usercomp;
                         this.addRow();
                    }else {
                        this.$message.error(res.resultMsg);
                        this.handleClose("done");
                    }
                }).catch(err => {
                    this.$message.error("获取当前用户的信息以及的时间异常，请稍后再试！");
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                    this.handleClose("done");
                })
            },
            addRow() {
                this.tableData.push({
                    chdrnum: "",
                    effdate01: "",
                    bmntdesc: "",
                    cownnum: "",
                    archtype: "4",
                    userid: this.userid,
                    effdate03: this.ldaDate,
                    flag01: "1",
                    chdrcoy:this.usercomp,
                    effdate02:this.ldaDate,
                    userProfile:this.userid,
                    glBusinessDate:this.ldaDate
                })
            },
            clearRow(){
                this.tableData = [{
                    chdrnum: "",
                    effdate01: "",
                    bmntdesc: "",
                    cownnum: "",
                    archtype: "4",
                    userid: this.userid,
                    effdate03: this.ldaDate,
                    flag01: "1",
                    chdrcoy:this.usercomp,
                    effdate02:this.ldaDate,
                    userProfile:this.userid,
                    glBusinessDate:this.ldaDate
                }]
            },
            validateSubmit(){
                this.$store.dispatch('loading/CHANGE_LOADING',true);
                this.$refs["dynamicValidateForm"].validate((valid) => {
                    if (!valid) {
                        this.$store.dispatch('loading/CHANGE_LOADING',false);
                        return false;
                    }else {
                        this.$store.dispatch('loading/CHANGE_LOADING',true);
                        if (this.tableData[0].flag01 == '1'){
                            //校验保单号是否存在
                            countEuruByChdrcoyRrtdatChdrnum(this.tableData[0].chdrcoy,this.tableData[0].chdrnum,this.tableData[0].effdate02).then(res => {
                                if (res.resultCode == constant.SUCCESS ) {
                                    if (res.data && res.data > 0){
                                        //保单存在
                                        //校验保单状态
                                        this.$store.dispatch('loading/CHANGE_LOADING',true);
                                        findMinFlag01EuruByChdrcoyRrtdatChdrnum(this.tableData[0].chdrcoy,this.tableData[0].chdrnum,this.tableData[0].effdate02).then(res => {
                                            this.$store.dispatch('loading/CHANGE_LOADING',false)
                                            if (res.resultCode == constant.SUCCESS) {
                                                if(res.data !="" && res.data !='0' && res.data !='9' && res.data !='A'){
                                                    this.$message.error("保单已被提核,上次提核处理未结案,因此不能对该单再次提核!");
                                                }else {
                                                    let flag = res.data;
                                                    //校验是否已被提核
                                                    this.$store.dispatch('loading/CHANGE_LOADING',true);
                                                    findClmnumEuruByChdrcoyRrtdatChdrnum(this.tableData[0].chdrcoy,this.tableData[0].chdrnum,this.tableData[0].effdate02).then(res => {
                                                        this.$store.dispatch('loading/CHANGE_LOADING',false);
                                                        if (res.resultCode == constant.SUCCESS && res.data) {
                                                            if (flag == '0' && res.data != "0"){
                                                                this.$message.error("保单已被提核,上次提核处理未结案,因此不能对该单再次提核!");
                                                            }else {
                                                                this.save();
                                                            }
                                                        }else {
                                                            this.$message.error(res.resultMsg);
                                                            this.$store.dispatch('loading/CHANGE_LOADING',false);
                                                        }
                                                    }).catch(err => {
                                                        this.$message.error("校验是否已被提核异常！");
                                                        this.$store.dispatch('loading/CHANGE_LOADING',false);
                                                    });
                                                }
                                            }else {
                                                this.$message.error(res.resultMsg);
                                                this.$store.dispatch('loading/CHANGE_LOADING',false);
                                            }
                                        }).catch(err => {
                                            this.$message.error("校验保单状态异常！");
                                            this.$store.dispatch('loading/CHANGE_LOADING',false);
                                        })
                                    }else {
                                        //保单不存在
                                        this.save();
                                    }
                                }else {
                                    this.$message.error(res.resultMsg);
                                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                                }
                            }).catch(err => {
                                this.$message.error("校验保单是否存在异常！");
                                this.$store.dispatch('loading/CHANGE_LOADING',false);
                            })
                        }else {
                            this.save();
                        }
                    }
                });
            },
            save(){
                this.$store.dispatch('loading/CHANGE_LOADING',true);
                addRenewal(this.tableData[0]).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                    if (res.resultCode == constant.SUCCESS) {
                        this.$message.success("保存成功");
                        this.tableData = [];
                        this.addRow();
                        this.handleClose("done");
                    }else {
                        this.$message.error(res.resultMsg);
                    }
                }).catch(err => {
                    this.$message.error("保存异常！");
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },
            //根据保单号码查询最小的Rrtdat
            findMinRrtdatFromCovrByChdrnum(){
                if (this.tableData[0].chdrnum == null || this.tableData[0].chdrnum == ""){
                    return;
                }
                this.$store.dispatch('loading/CHANGE_LOADING',true);
                findMinRrtdatFromCovrByChdrnum(this.tableData[0].chdrnum).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                    if (res.resultCode == constant.SUCCESS && res.data) {
                        this.tableData[0].effdate02 = res.data;
                    }else {
                        this.$message.error(res.resultMsg);
                    }
                }).catch(err => {
                    this.$message.error("取保单的Re-Rate Date异常！");
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },

        },
        watch: {},
        computed: {
            ...mapGetters(['tags']),

        },
        activated() {
            if (this.reload) {
                this.$store.dispatch('loading/CHNAGE_RELOAD', false)
                Object.assign(this.$data, this.$options.data());
            }
        }
    }
</script>

<style lang="less" scoped>
    .fillcontain {
        padding-bottom: 0;
    }

    .contain {
        background: #fff;
        padding: 10px;
        margin-bottom: 20px;
    }

    .pagination {
        padding: 10px 20px;
        text-align: right;
    }

    .operationIcon {
        // padding: 12px;
        border: 1px solid #e5e5e5;
        border-radius: 50%;
        width: 22px;
        height: 22px;
        display: inline-block;
        cursor: pointer;
        box-shadow: 0 0 3px #f9f9f9;

        .svg-icon {
            font-size: 14px;
            fill: #68a3fc;
        }
    }

    .search_container {
        padding: 20px 20px 0 20px;
        border-top: 1px solid #e1e1e1;
    }
</style>



